<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>通用后台管理系统</h2>
        <p>欢迎登录</p>
      </div>
      <el-form ref="loginForm" :model="form" :rules="rules" class="login-form" @submit.prevent="handleLogin">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" prefix-icon="User" size="large" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" prefix-icon="Lock" size="large"
            show-password />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="form.remember">记住我</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="handleLogin" class="login-btn">
            登录
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-link href="/login/forgetpassword" type="danger">忘记密码？</el-link>
          <el-link href="/register" type="primary" style="margin-left: 250px;">新用户注册</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/stores/auth'
import { usernameRules, passwordRules } from '@/utils/validationRules.js'
const router = useRouter()
const authStore = useAuthStore()

const loginForm = ref()
const loading = ref(false)
//初始表单数据
const form = reactive({
  username: 'admin',
  password: 'admin123',
  remember: true
})

const rules = {
  username: usernameRules,
  password: passwordRules
}
//用户登录√
const handleLogin = async () => {
  if (!loginForm.value) return

  try {
    await loginForm.value.validate()
    loading.value = true

    console.log('开始登录...', form.username)

    try {
      const user = await authStore.login(form.username, form.password, form.remember)

      ElMessage.success('登录成功')
      console.log('准备跳转到控制台...')

      // 跳转到控制台
      router.push('/dashboard')
    } catch (error) {
      console.error('登录失败:', error)
      if (error.message === 'invalid_credentials') {
        ElMessage.error('用户名或密码错误')
      } else if (error.message === 'account_locked') {
        ElMessage.error('账户已被锁定，请30分钟后重试')
      } else if (error.message === '账号未激活') {
        ElMessage.error('您的账号已被禁用，请联系管理员');
      }
      else {
        ElMessage.error('登录失败，请重试')
      }
    }
  } catch (validationError) {
    console.error('表单验证失败:', validationError)
  } finally {
    loading.value = false
  }
}

</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-box {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #333;
  margin-bottom: 10px;
  font-size: 24px;
  font-weight: 600;
}

.login-header p {
  color: #666;
  font-size: 14px;
  margin: 0;
}

.login-form {
  justify-content: center;
  width: 100%;
}

.login-btn {
  width: 100%;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-input__wrapper) {
  border-radius: 6px;
}

:deep(.el-button) {
  border-radius: 6px;
  font-weight: 500;
}
</style>